//
//  HHBaseViewController.m
//  Flower
//
//  Created by 花花 on 2016/12/7.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHBaseViewController.h"

#import "MBProgressHUD.h"
#import "MBProgressHUD+YISAddition.h"

#define RGBACOLOR(r,g,b,a)          [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
#define SYSTEM_VERSION        [[[UIDevice currentDevice] systemVersion] floatValue]
#define STATUSBAR_HEIGHT      [[UIApplication sharedApplication] statusBarFrame].size.height
#define NAVBAR_HEIGHT         (44.f + ((SYSTEM_VERSION >= 7) ? STATUSBAR_HEIGHT : 0))
@interface HHBaseViewController ()
{
    
    MBProgressHUD *successhud;
    MBProgressHUD *failedhud;
    MBProgressHUD *loadinghud;
    MBProgressHUD *costomhud;
    MBProgressHUD *nethud;
    UILabel *Netlabel;
}
@end

@implementation HHBaseViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    
}
- (void)hideHud {
    if (successhud) {
        [successhud hide:YES];
    }
    if (failedhud) {
        [failedhud hide:YES];
    }
    if (nethud) {
        [nethud hide:YES];
    }
    if (costomhud) {
        [costomhud hide:YES];
    }
    if (loadinghud) {
        [loadinghud hide:YES];
    }
}

- (void)showHudWithSuccessInfo {
    successhud = [MBProgressHUD HUDWithSuccess:@"成功"];
}

- (void)showHudWithFailedInfo {
    failedhud = [MBProgressHUD HUDWithSuccess:@"失败"];
}

- (void)showHudAutoHidesWithSuccessInfo {
    [MBProgressHUD HUDWithSuccess:@"成功" afterDelay:2];
}

- (void)showHudAutoHidesWithFailedInfo {
    [MBProgressHUD HUDWithFailure:@"失败" afterDelay:2];
}

- (void)showHudAutoHideString:(NSString *)info {
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = info;
    [hud show:YES];
    [hud hide:YES afterDelay:1.0];
}

- (void)showHudWithLoading {
    loadinghud = [MBProgressHUD HUDWithLoading:@"正在加载" inView:self.view];
    [loadinghud show:YES];
}

- (void)showHudWithLoadingWithCustomInfo:(NSString *)info  {
    loadinghud = [MBProgressHUD HUDWithLoading:info inView:self.view];
    
    [loadinghud show:YES];
    
    [loadinghud hide:YES afterDelay:1.0];
}

- (void)showHudWithCustomInfo:(NSString *)info {
    costomhud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:costomhud];
    costomhud.mode = MBProgressHUDModeText;
    costomhud.labelText = info;
    [costomhud show:YES];
}

- (void)showHudWithNetworkNotAvailable {
    nethud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:nethud];
    nethud.mode = MBProgressHUDModeText;
    nethud.labelText = @"网络不可用";
    [nethud show:YES];
}

- (void)showHudAutoHidesWithNetworkNotAvailable {
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = @"网络不可用";
    [hud show:YES];
    [hud hide:YES afterDelay:2.0];
}

- (void)showAlertWithString:(NSString *)info {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                    message:info
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

- (void)reportNetworkError:(NSError *)error {
    if ([error.domain isEqualToString:NSURLErrorDomain]) {
        switch (error.code) {
            case NSURLErrorCannotConnectToHost:
            case NSURLErrorCannotFindHost:
            case NSURLErrorNetworkConnectionLost:
            case NSURLErrorDNSLookupFailed:
            case NSURLErrorNotConnectedToInternet: {
                //网络连接错误
                MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
                [self.view addSubview:hud];
                hud.mode = MBProgressHUDModeText;
                hud.labelText = @"网络连接错误，请检查网络";
                [hud show:YES];
                [hud hide:YES afterDelay:2.0];
                
                break;
            }
            case NSURLErrorTimedOut: {
                //连接超时
                MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
                [self.view addSubview:hud];
                hud.mode = MBProgressHUDModeText;
                hud.labelText = @"连接超时";
                [hud show:YES];
                [hud hide:YES afterDelay:2.0];
                
                break;
            }
                
            default:
                break;
        }
    } else {
        MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:hud];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"服务器错误";
        [hud show:YES];
        [hud hide:YES afterDelay:2.0];
        
        //服务器错误
    }
}

/**
 *  提示用户网络状态(自动隐藏)
 */
- (void)showNetWorkStatusesAutoHides {
    //创建一个UILabel
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:12];
    
    // 设置背景
    label.text = @"无法连接到服务器，请检测网络是否畅通。";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor blackColor];
    
    //设置frame
    label.width = [UIScreen mainScreen].bounds.size.width;
    label.height = 35;
    label.x = 0;
    label.y = NAVBAR_HEIGHT - label.height;
    
    //添加到导航控制器的view
    //[self.navigationController.view addSubview:label];
    [self.navigationController.view
     insertSubview:label
     belowSubview:self.navigationController.navigationBar];
    
    //动画
    CGFloat duration = 0.75;
    // label.alpha = 0.0;
    [UIView animateWithDuration:duration
                     animations:^{
                         // 往下移动一个label的高度
                         label.transform = CGAffineTransformMakeTranslation(0, label.height);
                         // label.alpha = 1.0;
                     }
                     completion:^(BOOL finished) {  // 向下移动完毕
                         // 延迟delay秒后，再执行动画
                         CGFloat delay = 5.0;
                         
                         [UIView animateWithDuration:duration
                                               delay:delay
                                             options:UIViewAnimationOptionCurveEaseInOut
                                          animations:^{
                                              
                                              // 恢复到原来的位置
                                              label.transform = CGAffineTransformIdentity;
                                              // label.alpha = 0.0;
                                              
                                          }
                                          completion:^(BOOL finished) {
                                              
                                              // 删除控件
                                              [label removeFromSuperview];
                                          }];
                     }];
}

/**
 *  显示用户网络状态
 */
- (void)showNetWorkStatuses {
    if (Netlabel == nil) {
        //创建一个UILabel
        Netlabel = [[UILabel alloc] init];
        Netlabel.font = [UIFont systemFontOfSize:14];
        
        // 设置背景
        Netlabel.text = @"无"
        @"法连接到服务器，请检测网络是否畅通。";
        Netlabel.textAlignment = NSTextAlignmentCenter;
        Netlabel.textColor = [UIColor whiteColor];
        Netlabel.backgroundColor = RGBACOLOR(68, 123, 211, 0.5);
        
        //设置frame
        Netlabel.width = [UIScreen mainScreen].bounds.size.width;
        Netlabel.height = 35;
        Netlabel.x = 0;
        Netlabel.y = NAVBAR_HEIGHT - Netlabel.height;
    }
    
    //添加到导航控制器的view
    //[self.navigationController.view addSubview:label];
    [self.navigationController.view
     insertSubview:Netlabel
     belowSubview:self.navigationController.navigationBar];
    
    //动画
    CGFloat duration = 0.75;
    // label.alpha = 0.0;
    [UIView animateWithDuration:duration
                     animations:^{
                         // 往下移动一个label的高度
                         Netlabel.transform =
                         CGAffineTransformMakeTranslation(0, Netlabel.height);
                         // label.alpha = 1.0;
                     }];
}

/**
 * 隐藏
 */
- (void)HidensNetWorkStatuses {
    //动画
    CGFloat duration = 0.75;
    // label.alpha = 0.0;
    [UIView animateWithDuration:duration
                     animations:^{
                         // 往下移动一个label的高度
                         Netlabel.transform = CGAffineTransformIdentity;
                         // label.alpha = 1.0;
                     }
                     completion:^(BOOL finished) {
                         [Netlabel removeFromSuperview];
                     }];
}

@end
